package com.xc.ezplan.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xc.ezplan.admin.entity.AdminAliConfig;
import com.xc.ezplan.admin.entity.AdminAliContent;
import com.xc.ezplan.admin.mapper.AdminAliConfigMapper;
import com.xc.ezplan.admin.service.AdminAliContentService;
import com.xc.ezplan.admin.mapper.AdminAliContentMapper;
import com.xc.ezplan.admin.vo.AdminAliContentVo;
import com.xc.ezplan.core.base.BaseSearchBody;
import com.xc.ezplan.core.base.SearchCondition;
import com.xc.ezplan.core.domain.AliStorageConfig;
import com.xc.ezplan.core.utils.AliCloudUtil;
import com.xc.ezplan.core.utils.FileUtil;
import com.xc.ezplan.core.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lixingchen
 * @date 2022/12/04
 * @description 阿里云存储实现类
 */
@Service
@Primary
@RequiredArgsConstructor
public class AdminAliContentServiceImpl extends ServiceImpl<AdminAliContentMapper, AdminAliContent>
    implements AdminAliContentService{

    private final AdminAliConfigMapper adminAliConfigMapper;
    private final AdminAliContentMapper adminAliContentMapper;

    @SneakyThrows
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void uploadContent(MultipartFile file,Long configId) {
        AdminAliConfig adminAliConfig = adminAliConfigMapper.selectById(configId);
        AliStorageConfig aliStorageConfig = new AliStorageConfig();
        BeanUtils.copyProperties(adminAliConfig,aliStorageConfig);
        AliCloudUtil.upload(aliStorageConfig,file.getOriginalFilename(),file.getBytes());
        AdminAliContent adminAliContent = new AdminAliContent();
        adminAliContent.setName(file.getOriginalFilename());
        adminAliContent.setSize(FileUtil.getSize(Integer.parseInt(file.getSize()+"")));
        adminAliContent.setConfigId(configId);
        adminAliContent.setSuffix(FileUtil.getExtensionName(file.getOriginalFilename()));
        adminAliContentMapper.insert(adminAliContent);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteContent(List<Long> ids,Long configId) {
        AdminAliConfig adminAliConfig = adminAliConfigMapper.selectById(configId);
        AliStorageConfig aliStorageConfig = new AliStorageConfig();
        BeanUtils.copyProperties(adminAliConfig,aliStorageConfig);
        List<String> fileNames = adminAliContentMapper.selectList(new QueryWrapper<AdminAliContent>().lambda().
                in(AdminAliContent::getId, ids)).stream().map(content -> content.getName()).collect(Collectors.toList());
        AliCloudUtil.delete(aliStorageConfig,fileNames);
        adminAliContentMapper.deleteBatchIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateContent(AdminAliContentVo adminAliContentVo) {

    }

    @Override
    public Object pageQueryContent(BaseSearchBody<AdminAliContent> baseSearchBody) {
        Page<AdminAliContent> page = baseSearchBody.getPage();
        SearchCondition searchCondition = baseSearchBody.getSearchCondition();
        List<Date> createTime = searchCondition.getCreateTime();
        LambdaQueryWrapper<AdminAliContent> queryWrapper = new QueryWrapper<AdminAliContent>().lambda().like(
                StringUtils.isNotBlank(searchCondition.getKeyWord()), AdminAliContent::getName,
                searchCondition.getKeyWord()).eq(AdminAliContent::getConfigId,searchCondition.getAliConfigId());
        if (!CollectionUtils.isEmpty(createTime) && createTime.size() == 2) {
            queryWrapper.between(AdminAliContent::getUpdateTime, createTime.get(0), createTime.get(1));
        }
        return adminAliContentMapper.selectPage(page, queryWrapper);
    }

}




